package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunCameraConfigMapper;
import com.csun.cmny.provider.device.csun.camera.CameraMessageHandler;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.camera.CameraConfigDto;
import com.csun.cmny.provider.model.vo.csuncamera.CameraConfigVO;
import com.csun.cmny.provider.service.DmcCsunCameraConfigService;
import com.csun.cmny.provider.service.DmcLifeTransducerDefaultConfigService;
import com.csun.cmny.provider.service.DmcLifeTransducerRoomConfigService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DmcCsunCameraConfigServiceImpl extends BaseService<DmcCsunCameraConfig> implements DmcCsunCameraConfigService {

    @Resource
    private DmcCsunCameraConfigMapper dmcCsunCameraConfigMapper;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;
    @Resource
    private DmcLifeTransducerDefaultConfigService dmcLifeTransducerDefaultConfigService;
    @Resource
    private CameraMessageHandler cameraMessageHandler;

    @Override
    public DmcCsunCameraConfig queryConfigByRoomIdAndGroupId(Integer roomId, Long groupId) {

        DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig = null;
        DmcCsunCameraConfig dmcCsunCameraConfig = null;

        if (PublicUtil.isNotEmpty(roomId)) {
            dmcLifeTransducerRoomConfig = dmcLifeTransducerRoomConfigService.selectByKey(roomId);
        }

        if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
            // 从该机构的配置中获取
            DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig =
                    dmcLifeTransducerDefaultConfigService.selectByKey(groupId);
            if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
                return null;
            } else {
                dmcCsunCameraConfig = new DmcCsunCameraConfig();
                dmcCsunCameraConfig.setTumbleMeasuringFrame(dmcLifeTransducerDefaultConfig.getTumbleMeasuringFrame());
                dmcCsunCameraConfig.setWalkMeasuringEndTime(dmcLifeTransducerDefaultConfig.getWalkMeasuringEndTime());
                dmcCsunCameraConfig.setWalkMeasuringStartTime(dmcLifeTransducerDefaultConfig.getWalkMeasuringStartTime());
                dmcCsunCameraConfig.setWalkMeasuringTimeInterval(dmcLifeTransducerDefaultConfig.getWalkMeasuringTimeInterval());
                dmcCsunCameraConfig.setCameraCheckTime(dmcLifeTransducerDefaultConfig.getCameraCheckTime());
                dmcCsunCameraConfig.setBeforeDuration(dmcLifeTransducerDefaultConfig.getBeforeDuration());
                dmcCsunCameraConfig.setAfterDuration(dmcLifeTransducerDefaultConfig.getAfterDuration());
            }
        } else {
            dmcCsunCameraConfig = new DmcCsunCameraConfig();
            dmcCsunCameraConfig.setTumbleMeasuringFrame(dmcLifeTransducerRoomConfig.getTumbleMeasuringFrame());
            dmcCsunCameraConfig.setWalkMeasuringEndTime(dmcLifeTransducerRoomConfig.getWalkMeasuringEndTime());
            dmcCsunCameraConfig.setWalkMeasuringStartTime(dmcLifeTransducerRoomConfig.getWalkMeasuringStartTime());
            dmcCsunCameraConfig.setWalkMeasuringTimeInterval(dmcLifeTransducerRoomConfig.getWalkMeasuringTimeInterval());
            dmcCsunCameraConfig.setCameraCheckTime(dmcLifeTransducerRoomConfig.getCameraCheckTime());
            dmcCsunCameraConfig.setBeforeDuration(dmcLifeTransducerRoomConfig.getBeforeDuration());
            dmcCsunCameraConfig.setAfterDuration(dmcLifeTransducerRoomConfig.getAfterDuration());
        }

        return dmcCsunCameraConfig;
    }

    @Override
    public Integer saveOrUpdate(DmcCsunCameraConfig dmcCsunCameraConfig) {
        // TODO
        // return dmcCsunCameraConfigMapper.saveOrUpdate(dmcCsunCameraConfig);
        int result = 0;
        DmcCsunCameraConfig record = dmcCsunCameraConfigMapper.selectByPrimaryKey(dmcCsunCameraConfig.getDeviceId());
        if (PublicUtil.isEmpty(record)) {
            result = dmcCsunCameraConfigMapper.insertSelective(dmcCsunCameraConfig);
        } else {
            result = dmcCsunCameraConfigMapper.updateByPrimaryKeySelective(dmcCsunCameraConfig);
        }
        return result;
    }

    @Override
    public Integer setCsunCameraConfig(CameraConfigDto dto, LoginAuthDto loginAuthDto) {

        List<Long> deviceIdList = dto.getDeviceIdList();
        int i = 0;

        for (Long deviceId : deviceIdList) {

            //1.下发时间指令
            DmcCsunCameraConfig dmcCsunCameraConfig = selectByKey(deviceId);
            DmcCsunCameraConfig config = new DmcCsunCameraConfig();

            config.setDeviceId(deviceId);
            BeanUtils.copyProperties(dto,config);
            config.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
            config.setLastOperator(loginAuthDto.getUserName());
            config.setLastOperatorId(loginAuthDto.getUserId());

            if(dmcCsunCameraConfig == null){
                config.setCreator(loginAuthDto.getUserName());
                config.setCreatorId(loginAuthDto.getUserId());
                dmcCsunCameraConfigMapper.insertSelective(config);
            }else{
                dmcCsunCameraConfigMapper.updateByPrimaryKeySelective(config);
            }

            /**
             * @date    2020-01-09
             * @author  chisj
             * @desc    下发摄像头配置
             */
            cameraMessageHandler.sendCameraConfigMsg(deviceId,
                    dto.getWalkMeasuringStartTime(),
                    dto.getWalkMeasuringEndTime(),
                    dto.getWalkMeasuringTimeInterval(),
                    dto.getTumbleMeasuringFrame(),
                    dto.getCameraCheckTime(),
                    dto.getBeforeDuration(),
                    dto.getAfterDuration());

            i++;
        }
        return i;
    }

    @Override
    public CameraConfigVO getCsunCameraConfig(Long deviceId) {

        CameraConfigVO vo = new CameraConfigVO();
        DmcCsunCameraConfig dmcCsunCameraConfig = dmcCsunCameraConfigMapper.selectByPrimaryKey(deviceId);
        if(dmcCsunCameraConfig != null){
            BeanUtils.copyProperties(dmcCsunCameraConfig,vo);
        }
        return vo;
    }
}

